package si.yun.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import si.yun.common.GuavaCommon;
import si.yun.entity.NavigationUserInfo;
import si.yun.service.NavigationService;
import si.yun.utils.GuavaUtils;
import si.yun.utils.JsonData;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author: DZQ
 * @description: TODO
 * @date: 2021/7/28 20:11
 * @return
 */
@RestController
@RequestMapping("/Navigation")
public class NavigationWeb {

    @Autowired
    private NavigationService service;

   @Autowired
    private GuavaUtils GuavaUtil;
  /**
   * @author: DZQ
   * @description:  从数据库获取导航栏字段 Guava缓存
   * @date: 2021/7/28 20:12
   * @return JsonData
   */
    @PostMapping("/selNavigation")
    public JsonData selNavigation(){
        try{
            Object obj = GuavaUtil.getTenCacheBuild().get(String.valueOf(GuavaCommon.Navigation), () -> {
                List<Map<String, Object>> getNavigation = service.selNavigation();
                return getNavigation;
            });
            if(obj instanceof List){
                List<Map<String,Object>> list = (List<Map<String,Object>>) obj;
                return list.size()!=0?JsonData.buildSuccess(list):JsonData.buildError("服务器端错误");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * @Description  获取登录信息
     * @return si.yun.utils.JsonData 
     * @Date 2021/9/13 11:53
     **/
    @PostMapping("/selUserInfo")
    public NavigationUserInfo selUserInfo(Integer userId) {
        NavigationUserInfo navigationUserInfo = service.selUserInfo(userId);
        return navigationUserInfo;
    }
    /**
     * @author: DZQ
     * @description: 账号推出并销毁session周期
     * @date: 2021/7/30 20:10
     * @return 登陆页面
     */
    //退出
    @PostMapping("/loginOut")
    public boolean loginOut(HttpSession session, HttpServletResponse response) {
        //删除session的指定属性
        session.removeAttribute("user");
        session.invalidate();
        return true;
    }

}